package com.qyhl.module_home.new_message.detail;


import com.qyhl.module_home.common.HomeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.config.MessageDetailBean;

import java.util.List;

public class MessageDetailModel implements MessageDetailContract.MessageDetailModel {
    private MessageDetailPresenter mPresenter;

    MessageDetailModel(MessageDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getMessageList(String phone, String time, final String msgId, String type) {

        EasyHttp.get(HomeUrl.GET_MESSAGE_LIST)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("devType", "Android")
                .params("phone", phone)
                .params("time", time)
                .params("msgId", msgId)
                .params("type", type)
                .execute(new SimpleCallBack<List<MessageDetailBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            if (msgId.equals("0")) {
                                mPresenter.setError(2, "暂无任何消息！");
                            } else {
                                mPresenter.setError(3, "暂无更多消息！");
                            }
                        } else {
                            if (msgId.equals("0")) {
                                mPresenter.setError(4, "请求消息失败！");
                            } else {
                                mPresenter.setError(5, "请求消息失败！");
                            }
                        }

                    }

                    @Override
                    public void onSuccess(List<MessageDetailBean> messageDetailBeans) {
                            if(messageDetailBeans != null && messageDetailBeans.size() > 0){
                                if (msgId.equals("0")) {
                                    mPresenter.setMessageList(messageDetailBeans, false);
                                } else {
                                    mPresenter.setMessageList(messageDetailBeans, true);
                                }
                            }else{
                                if (msgId.equals("0")) {
                                    mPresenter.setError(2, "暂无任何消息！");
                                } else {
                                    mPresenter.setError(3, "暂无更多消息！");
                                }
                            }

                    }
                });
        
    }
}
